package leetcode.L54;

import java.util.ArrayList;
import java.util.List;


class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> list = new ArrayList<>();
        //从最外层开始遍历,然后网内层遍历
        int m = matrix.length;
        int n = matrix[0].length;
        int left=0;
        int right = n-1;
        int top = 0;
        int bottom = m-1;
        while (left<=right && top<=bottom) {
            for (int i = left; i <= right; i++) {
                list.add(matrix[top][i]);
            }
            for (int i = top+1; i <= bottom; i++) {
                list.add(matrix[i][right]);
            }
            //这里上面已经完成了宽或者高为1的遍历
            if (left < right && top < bottom) {
                for (int i = right-1; i > left ; i--) {
                    list.add(matrix[bottom][i]);
                }
                for (int i = bottom; i >= top+1 ; i--) {
                    list.add(matrix[i][left]);
                }
            }
            top++;
            bottom--;
            left++;
            right--;
        }
        return list;
    }
}